package com.jodie.store.controller;

import com.jodie.store.controller.ex.*;
import com.jodie.store.service.ex.*;
import com.jodie.store.util.JsonResult;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpSession;
import java.nio.file.AccessDeniedException;

/**
 * 控制层类的基类
 * @author Jodie
 * @date 2023/2/3-12:32
 */
public class BaseController {
    public static final int OK = 200;

    // 用于统一处理抛出的异常，结果直接返回前端
    // @ExceptionHandler参数列表加入class类型基类异常
    @ExceptionHandler({ServiceException.class,FileUploadException.class})
    public JsonResult<Void> handleException(Throwable e){
        JsonResult<Void> result = new JsonResult<>(e);
        if (e instanceof UsernameDuplicatedException) {
            result.setState(4000);
            result.setMessage("用户名被占用异常");
        } else if (e instanceof InsertException) {
            result.setState(5000);
            result.setMessage("注册异常");
        } else if (e instanceof UserNotFoundException) {
            result.setState(5001);
            result.setMessage("用户数据不存在异常");
        } else if (e instanceof PasswordNotMatchException) {
            result.setState(5002);
            result.setMessage("用户密码错误异常");
        } else if (e instanceof UpdateException) {
            result.setState(5003);
            result.setMessage("更新数据时产生异常");
        } else if (e instanceof DeleteException) {
            result.setState(5004);
            result.setMessage("删除数据时产生异常");
        } else if (e instanceof FileEmptyException) {
            result.setState(6000);
            result.setMessage("文件空异常");
        } else if (e instanceof FileSizeException) {
            result.setState(6001);
            result.setMessage("文件大小异常");
        } else if (e instanceof FileTypeException) {
            result.setState(6002);
            result.setMessage("文件格式异常");
        } else if (e instanceof FileStateException) {
            result.setState(6003);
            result.setMessage("文件状态异常");
        } else if (e instanceof FileUploadIOException) {
            result.setState(6004);
            result.setMessage("文件上传异常");
        } else if (e instanceof AddressCountLimitException) {
            result.setState(4003);
            result.setMessage("用户的收货地址超出上限异常");
        } else if (e instanceof AddressNotFoundException) {
            result.setState(4004);
            result.setMessage("用户收获地址数据不存在异常");
        } else if (e instanceof AccessDeniedException) {
            result.setState(4005);
            result.setMessage("用户收获地址数据非法访问异常");
        } else if (e instanceof ProductNotFoundException) {
            result.setState(4006);
            result.setMessage("商品数据不存在异常");
        } else if (e instanceof ProductNotFoundException) {
            result.setState(4007);
            result.setMessage("购物车数据不存在异常");
        }
        return result;
    }

    // 获取当前登录uid，参数session对象，返回uid
    protected final Integer getuidFromSession(HttpSession session){
        return Integer.valueOf(session.getAttribute("uid").toString());
    }
    // 获取当前登录用户的username，参数session，返回用户名
    protected final String getUsernameFromSession(HttpSession session){
        return session.getAttribute("username").toString();
    }
}
